//
// Created by root on 20-1-15.
//

#include "ap_changer.h"

AppProcess::AppProcess(const char *file_buf,file_size buf_size) {
    mFileBuf=new LengthBuf(file_buf,buf_size);
}

void AppProcess::Parse() {
    // 解析Elf_Ehdr
    u8 off = (u8)mFileBuf->mBuf;
    Elf32_Ehdr* ehdr = (Elf32_Ehdr*)off;
    mInfo.ehdr=ehdr;

    // 解析Elf_Shdr
    Elf32_Shdr* shdrs = (Elf32_Shdr*)(off + ehdr->e_shoff);
    mInfo.shdrs=shdrs;

    // 解析Dynamic Section
    Elf32_Dyn* dyn= nullptr;
    u4 dynSize=0;
    for (u2 i = 0; i < ehdr->e_shnum; i++) {
        if(shdrs[i].sh_type == SHT_DYNAMIC) {
            dyn = (Elf32_Dyn*)(off + shdrs[i].sh_offset);
            dynSize = shdrs[i].sh_size / shdrs[i].sh_entsize;
        }
    }
    mInfo.dyn=dyn;
    mInfo.dynSize=dynSize;
    // 解析Dynamic Strtab
    const char* strtab= nullptr;
    for (u4 i = 0; i < dynSize; i++) {
        if (dyn[i].d_tag == DT_STRTAB) {
            strtab = reinterpret_cast<const char*>(off + dyn[i].d_un.d_ptr);
        }
    }
    mInfo.strtab=strtab;
}
